<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>
<div class="page-container">
    <div class="ibox-content" id="toolbar">
        <form action="" class="form-inline" id="form" data-url="">
            <div class="form-group">
                <label class="control-label" for="key">请选择:</label>
                <select id="time-key" class="form-control" name="time-key">
                    <option value="cre">创建时间</option>
                    <option value="upd">更新时间</option>
                </select>
            </div>
            <div class="form-group">
                <label class="control-label">
                    <label class="control-label">开始时间：
                        <input type="text" class="form-control date-search" id="start_time" placeholder="">
                    </label>
                </label>
            </div>
            <div class="form-group">
                <label class="control-label">
                    <label class="control-label">结束时间：
                        <input type="text" class="form-control date-search" id="end_time" placeholder="">
                    </label>
                </label>
            </div>
            <div class="form-group">
                <label class="control-label" for="key">请选择:</label>
                <select id="key" class="form-control" name="key">
                    <option value="u_id">用户ID</option>
                    <option value="nickname">用户名</option>
                    <option value="incordec_status">收支状态</option>
                    <option value="incordec_type">收支类型</option>
                    <option value="auditing">审核状态</option>
                    <option value="status">纪录状态</option>
                </select>
            </div>
            <div class="form-group">
                <label class="control-label" for="value">输入:</label>
                <input class="form-control" id="value" type="text" name="value">
            </div>
            <button type="button" class="btn btn-primary" id="eventQuery">查询</button>
            <button type="button" onclick="location.replace(location.href);" class="btn btn-success ">刷新</button>
        </form>
    </div>
    <div class="float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">卡片收支列表</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div class=" tab-pane active" id="tab-1">
                        <table id="tb_departments"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}
<script>
    $(document).ready(function () {
        initTable();
    });

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/CardInoutIndexData',         //请求后台的URL（*）
            dataField: 'data',
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showToggle: true,
            showRefresh: true,
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 20,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showPaginationSwitch: true,
            minimumCountColumns: 2,             //最少允许的列数
            toolbar: '#toolbar',
            columns: [ {
                field: 'id',
                title: 'ID',
                sortable: true
            }, {
                field: 'u_id',
                title: '用户ID'
            }, {
                field: 'nickname',
                title: '用户名'
            },{
                field: 'name',
                title: '卡片名称'
            },{
                field: 'card_num',
                title: '卡片数量'
            }, {
                field: 'incordec_status',
                title: '收支状态',
                formatter: function (value, row, index) {
                    var arr=new Array();
                    arr[1]="增加";arr[2]="消耗";
                    return arr[row.incordec_status] != null ? arr[row.incordec_status] : '未知';
                }
            },  {
                field: 'incordec_type',
                title: '收支类型',
                formatter: function (value, row, index) {
                    var arr=new Array();
                    arr[1]="系统赠送";arr[2]="签到赠送";arr[3]="他人赠送";arr[4]="邀请赠送";arr[5]="送给他人";arr[6]="合成座驾";arr[7]="合成礼物";arr[8]="合成礼袋";
                    return arr[row.incordec_type] != null ? arr[row.incordec_type] : '未知状态';
                }
            }, {
                field: 'event',
                title: '关联事件'
            }, {
                field: 'auditing',
                title: '审核结果',
                formatter: function (value, row, index) {
                    var arr=new Array();
                    arr[2]="已审核";arr[3]="未审核";
                    return arr[row.auditing] != null ? arr[row.auditing] : '未知状态';
                }
            }, {
                field: 'status',
                title: '纪录状态',
                formatter: function (value, row, index) {
                    var arr=new Array();
                    arr[1]="进行中";arr[2]="成功";arr[3]="失败";
                    return arr[row.status] != null ? arr[row.status] : '未知状态';
                }
            }, {
                field: 'create_time',
                title: '创建时间'
            },{
                field: 'update_time',
                title: '更新时间'
            },{
                field: 'Button',
                title: '操作',
                events: operateEvents,
                formatter: operateFormatter
            }]
        });
    }

    //得到查询的参数

    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.key = $('#key').val();
        params.value = $('#value').val();
        params.time_key = $('#time-key').val();
        params.start_time = $('#start_time').val();
        params.end_time = $('#end_time').val();
        return params;
    }

    function operateFormatter(value, row, index) {
        if(row.card_id == 13 && row.auditing == 3 && row.status == 1)
            return [
//                '<button type="button"  class="card-agree btn btn-primary btn-sm" style="margin-right:15px;">同意申请</button>',
//                '<button type="button"  class="card-refuse btn btn-primary btn-sm" style="margin-right:15px;">拒绝申请</button>',
                '<button type="button"  class="add-gift btn btn-primary btn-sm" style="margin-right:15px;">放入礼物</button>',
                '<button type="button"  class="user-detail btn btn-primary btn-sm" style="margin-right:15px;">查看用户信息</button>'
            ].join('');
        else
            return [
                '<button type="button"  class="user-detail btn btn-primary btn-sm" style="margin-right:15px;">查看用户信息</button>'
            ].join('');
    }

    window.operateEvents = {
        //查看用户信息
        'click .user-detail': function (e, value, row, index) {
            console.log(row.user_id);
            layer.open({
                type: 2,
                title: ['用户信息', 'font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '600px'],
                content:"/UserInfo?user_id="  + row.user_id,
                offset: 'auto',
                scrollbar: false
            });
        },
        //同意申请
        'click .card-agree': function (e, value, row, index) {
            $.ajax({
                type: 'get',
                dataType: 'json',
                url:"/CardInoutAgree",
                data: {'id': row.id},
                success: function (re) {
                    if (re.code === 200) {
                        layer.msg(re.msg, {icon: 1, time: 2000});
                        window.location.reload();
                    } else {
                        layer.alert(re.msg);
                        window.location.reload();
                    }
                },
                error: function (data) {
                    console.log(data.msg);
                }
            });
        },
        //拒绝申请
        'click .card-refuse': function (e, value, row, index) {
            $.ajax({
                type: 'get',
                dataType: 'json',
                url:"/CardInoutRefuse",
                data: {'id': row.id},
                success: function (re) {
                    if (re.code === 200) {
                        layer.msg(re.msg, {icon: 1, time: 2000});
                        window.location.reload();
                    } else {
                        layer.alert(re.msg);
                        window.location.reload();
                    }
                },
                error: function (data) {
                    console.log(data.msg);
                }
            });
        },

        //添加礼物
        'click .add-gift': function (e, value, row, index) {
            layer.open({
                type: 2,
                title: ['添加礼物','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['1000px', '600px'],
                content:"/CardAddGift?user_id=" + row.user_id + '&incordec_id='+ row.id  ,
                offset:'auto',
                scrollbar:false,
                end: function () {
                    $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });
        }
    };

    // 搜索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });

    });
    //时间搜索插件
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        lay('.date-search').each(function () {
            laydate.render({
                elem: this,
                type: 'datetime',
                trigger: 'click'
            });
        });
    });
</script>
</body>
</html>